package com.ll.gulimall.order.listener;

import com.ll.gulimall.order.entity.OrderEntity;
import com.ll.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order.release.order")
public class OrderListener {

    @Autowired
    OrderService orderService;

    /**
     * 能收到这个消息 说明死信已经转发过来了 订单超时了
     *
     * @param message
     * @param orderEntity
     * @param channel
     */
    @RabbitHandler
    public void closeOrder(Message message, OrderEntity orderEntity, Channel channel) throws IOException {

        try {
            orderService.closeOrder(message, orderEntity, channel);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }
}
